package com.artisan.provider.service.impl;


import com.artisan.provider.domain.TPromotionSeckill;
import com.artisan.provider.mapper.*;
import com.artisan.provider.service.ProductService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    TCategoryMapper tCategoryMapper;

    @Autowired
    TEvaluateMapper tEvaluateMapper;

    @Autowired
    TGoodsCoverMapper tGoodsCoverMapper;

    @Autowired
    TGoodsDetailMapper tGoodsDetailMapper;

    @Autowired
    TGoodsMapper tGoodsMapper;

    @Autowired
    TGoodsParamMapper tGoodsParamMapper;

    @Autowired
    TPromotionSeckillMapper tPromotionSeckillMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<TPromotionSeckill> findUnstartSecKill() {
        return tPromotionSeckillMapper.findUnstartSecKill();
    }

    @Override
    public void update(TPromotionSeckill tPromotionSeckill) {
        tPromotionSeckillMapper.updateByPrimaryKeySelective(tPromotionSeckill);
    }

    /**
     * 秒杀活动实现
     * @param psId
     * @param userId
     * @param num
     */
    public void promotionSecKillService(int psId,String userId,int num){


        TPromotionSeckill byId = tPromotionSeckillMapper.findById(psId);

        if(byId == null){
            //秒杀活动不存在
            throw new SecurityException("秒杀活动不存在");
        }

        if(byId.getStatus() == 0){
            throw new SecurityException("秒杀活动还没开始");
        }

        if(byId.getStatus() == 2){
            throw new SecurityException("秒杀活动已经结束");
        }

        /**从redis的左侧弹出 弹出来并接受值*/
        Integer goodsId = (Integer) redisTemplate.opsForList().leftPop("seckill:count:" + byId.getPsId());

        //判断redis里面是否有值
        if(goodsId != null){
            //获取redis userId 判断是否已经抢购过
            Boolean member = redisTemplate.opsForSet().isMember("seckill:users:" + byId.getPsId(), userId);
            if (member){

                redisTemplate.opsForList().rightPush("seckill:count:"+byId.getPsId(),byId.getGoodsId());
                throw new SecurityException("已经抢过了");
            }else{
                System.out.println("恭喜您抢到商品了,快去下单.......");
                redisTemplate.opsForSet().add("seckill:users:"+byId.getPsId(),userId);
            }

        }else{
            throw new SecurityException("该商品已被抢空,下次再来");
        }
    }

    @Override
    public List<TPromotionSeckill> findExpriceSecKill() {
        return tPromotionSeckillMapper.findExpriceSecKill();
    }

    @Override
    public String sendOrderToQueue(String userId) {
        System.out.println("准备向队列发送消息");
        //订单基本信息
        Map map = new HashMap<>();
        map.put("userid",userId);
        String orderNo = UUID.randomUUID().toString();
        map.put("orderNo",orderNo);
        //附加额外的订单信息
        //交换机  将获取到的订单信息发送给交换机
        rabbitTemplate.convertAndSend("exchange-order",null,map);

        return orderNo;
    }
}
